package com.example.pclimb.model;

import androidx.annotation.NonNull;
import androidx.room.Entity;
import androidx.room.PrimaryKey;
import androidx.room.Ignore;

/**
 * 用户实体类
 */
@Entity(tableName = "users")
public class User {
    @PrimaryKey(autoGenerate = true)
    private int id;
    private String username;
    private String password;
    private String phone;
    private String email;
    private int userType; // 1-普通用户, 2-陪爬向导, 3-管理员
    private float rating; // 评分（对于向导）
    private int ratingCount; // 评分数量
    private String avatarUrl; // 头像URL
    private String nickname; // 用户昵称，兼容旧代码
    private String token; // 认证令牌，兼容旧代码
    private String certificationUrl; // 证书图片URL，兼容旧代码
    
    // 向导申请相关字段
    private int guideApplication; // 0-未申请, 1-待审核, 2-已批准, 3-已拒绝
    private String experience; // 登山经验
    private String certification; // 证书信息
    // 兼容旧代码的布尔标记
    private boolean isGuideApplied; // 是否申请了陪爬资格
    private boolean isGuideApproved; // 陪爬资格是否通过审核

    // 用户类型常量
    public static final int TYPE_USER = 1;     // 普通用户
    public static final int TYPE_GUIDE = 2;    // 陪爬向导
    public static final int TYPE_ADMIN = 3;    // 管理员
    
    // 向导申请状态常量
    public static final int APPLICATION_NONE = 0;     // 未申请
    public static final int APPLICATION_PENDING = 1;  // 申请中
    public static final int APPLICATION_APPROVED = 2; // 已批准
    public static final int APPLICATION_REJECTED = 3; // 已拒绝

    @Ignore
    public User() {
    }

    @Ignore
    public User(String username, String password) {
        this.username = username;
        this.password = password;
        this.userType = TYPE_USER;
        this.rating = 0;
        this.ratingCount = 0;
        this.guideApplication = APPLICATION_NONE;
        this.nickname = username; // 默认使用用户名作为昵称
        this.isGuideApplied = false;
        this.isGuideApproved = false;
    }

    @Ignore
    public User(String username, String password, int userType) {
        this.username = username;
        this.password = password;
        this.userType = userType;
        this.rating = 0;
        this.ratingCount = 0;
        this.guideApplication = APPLICATION_NONE;
        this.nickname = username; // 默认使用用户名作为昵称
        this.isGuideApplied = false;
        this.isGuideApproved = (userType == TYPE_GUIDE);
    }


    public User(String username, String password, String phone, String email, int userType) {
        this.username = username;
        this.password = password;
        this.phone = phone;
        this.email = email;
        this.userType = userType;
        this.rating = 0;
        this.ratingCount = 0;
        this.guideApplication = APPLICATION_NONE;
        this.nickname = username; // 默认使用用户名作为昵称
        this.isGuideApplied = false;
        this.isGuideApproved = (userType == TYPE_GUIDE);
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    // 兼容旧代码，提供旧的方法名
    public String getPhoneNumber() {
        return phone;
    }

    // 兼容旧代码，提供旧的方法名
    public void setPhoneNumber(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public int getUserType() {
        return userType;
    }

    public void setUserType(int userType) {
        this.userType = userType;
        // 更新兼容属性
        if (userType == TYPE_GUIDE) {
            this.isGuideApproved = true;
        }
    }

    public float getRating() {
        return rating;
    }

    public void setRating(float rating) {
        this.rating = rating;
    }

    public int getRatingCount() {
        return ratingCount;
    }

    public void setRatingCount(int ratingCount) {
        this.ratingCount = ratingCount;
    }

    public String getAvatarUrl() {
        return avatarUrl;
    }

    public void setAvatarUrl(String avatarUrl) {
        this.avatarUrl = avatarUrl;
    }

    // 兼容旧代码，获取头像
    public String getAvatar() {
        return avatarUrl;
    }

    // 兼容旧代码，设置头像
    public void setAvatar(String avatar) {
        this.avatarUrl = avatar;
    }

    public String getNickname() {
        return nickname != null ? nickname : username;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public int getGuideApplication() {
        return guideApplication;
    }

    public void setGuideApplication(int guideApplication) {
        this.guideApplication = guideApplication;
        // 更新兼容属性
        if (guideApplication == APPLICATION_PENDING) {
            this.isGuideApplied = true;
            this.isGuideApproved = false;
        } else if (guideApplication == APPLICATION_APPROVED) {
            this.isGuideApplied = true;
            this.isGuideApproved = true;
            this.userType = TYPE_GUIDE;
        } else if (guideApplication == APPLICATION_REJECTED) {
            this.isGuideApplied = true;
            this.isGuideApproved = false;
        } else {
            this.isGuideApplied = false;
            this.isGuideApproved = false;
        }
    }

    public String getExperience() {
        return experience;
    }

    public void setExperience(String experience) {
        this.experience = experience;
    }

    public String getCertification() {
        return certification;
    }

    public void setCertification(String certification) {
        this.certification = certification;
    }

    public String getCertificationUrl() {
        return certificationUrl;
    }

    public void setCertificationUrl(String certificationUrl) {
        this.certificationUrl = certificationUrl;
    }

    public boolean isGuideApplied() {
        return isGuideApplied || guideApplication > APPLICATION_NONE;
    }

    public void setGuideApplied(boolean guideApplied) {
        this.isGuideApplied = guideApplied;
        // 更新新属性
        if (guideApplied && guideApplication == APPLICATION_NONE) {
            this.guideApplication = APPLICATION_PENDING;
        } else if (!guideApplied) {
            this.guideApplication = APPLICATION_NONE;
        }
    }

    public boolean isGuideApproved() {
        return isGuideApproved || guideApplication == APPLICATION_APPROVED || userType == TYPE_GUIDE;
    }

    public void setGuideApproved(boolean guideApproved) {
        this.isGuideApproved = guideApproved;
        // 更新新属性
        if (guideApproved) {
            this.guideApplication = APPLICATION_APPROVED;
            this.userType = TYPE_GUIDE;
        } else if (isGuideApplied()) {
            this.guideApplication = APPLICATION_PENDING;
        }
    }

    /**
     * 检查是否是陪爬向导
     */
    public boolean isGuide() {
        return userType == TYPE_GUIDE;
    }

    /**
     * 检查是否是管理员
     */
    public boolean isAdmin() {
        return userType == TYPE_ADMIN;
    }

    /**
     * 更新评分
     * @param newRating 新评分
     */
    public void updateRating(float newRating) {
        float totalRating = this.rating * this.ratingCount;
        this.ratingCount++;
        this.rating = (totalRating + newRating) / this.ratingCount;
    }

    @NonNull
    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", userType=" + userType +
                '}';
    }
} 